package com.thinkfit.jy.utils;

import com.blankj.utilcode.util.GsonUtils;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.inuker.bluetooth.library.utils.ByteUtils;
import com.thinkfit.jy.bean.EquipmentInfoBean;
import com.thinkfit.jy.bean.EquipmentStateBean;
import com.thinkfit.jy.bean.SportsDataBean;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.serialport.SportStatus;


import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 运动数据处理工具类
 */
public class MotionDataUtils {
    private static final String TAG  = "MotionDataUtils ------------->";
    static MotionDataUtils motionDataUtils;
    private SerialPortManager.CurrentInfo mCurrentInfo;

    public int deviceSpeed = 1;
    int heartRate;

    private SportStatus mRunStatus = SportStatus.STOP;

    public static String mCurrentDeviceType = DeviceTypeUtils.TREADMILL;

    private static final int GET_EQUIPMENT_COUNT_DOWN = 400;
    private static final int GET_SPORTS_COUNT_DOWN = 660;
    private static final int OTHER_COUNT_DOWN = 300;

    private static final int LOOP_NUM = 3;

    Disposable coundDisposable;

    public static MotionDataUtils getInstance(){
        if(motionDataUtils == null){
            motionDataUtils = new MotionDataUtils();
        }
        return motionDataUtils;
    }


    /**
     * 响应信息解析
     * @param value
     */
    public void motionDataResponse(byte[] value){
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" ddddddddddddddddddddddd value : "+ ByteUtils.byteToString(value));
        if(value == null || value.length <= 4 ){
            return;
        }
        byte b = SerialPortManager.getInstance().UartFCS(value, value.length-2);
        if(b != value[value.length-2]){
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    丢包数据 : " + ByteUtils.byteToString(value));
            return;
        }
        String instruct1 = Long.toString(value[1] & 0xff, 16);
        String instruct2 = Long.toString(value[2] & 0xff, 16);//转换会把前面的"0"给略掉，需要可以用StringBuffer拼上
//        System.out.println(TAG+" ddddddddddddddddddddddd value[1] : "+instruct1+"   value[2] :"+instruct2);

        if(instruct1.equals("41")&& instruct2.equals("2")){
            //设备参数
            equipmentInfo(value);

        }else if(instruct1.equals("50")&& instruct2.equals("0")){
            //获取设备品牌
            getBrandInfo(value);

        }else if(instruct1.equals("42")){
            //获取设备状态
            getEquipmentState(value);

        }else if(instruct1.equals("43")&&instruct2.equals("1")){
            //获取运动数据
            getSportsData(value);
        }else if(instruct1.equals("44")&&instruct2.equals("1")){
            //准备就绪
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面准备就绪状态  ");
            if(mRunStatus == SportStatus.START){
                return;
            }
            mRunStatus = SportStatus.START;
            int countdown = formatHexInt(Long.toString(value[3] & 0xff, 16));
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+"ddddddddddddddddddddddd  countdown : "+countdown);
            if (countdown > 0) {
                HashMap<String, Object> map = new HashMap<>();
                map.put(EventBusEvent.START_RUN, countdown);
                EventBus.getDefault().post(map);
            }else {

                if(DeviceTypeUtils.connectDeviceType() == 0){
                    BluetoothOrder.getInstance().bluetoothOrderWrite(BluetoothOrder.EQUIPMENT_START_OR_CONTINUE);
                }else {
                    SerialPortManager.Cmd cmd = new SerialPortManager.Cmd();
                    cmd.bytes = BluetoothOrder.EQUIPMENT_START_OR_CONTINUE;
                    SerialPortManager.getInstance().addCmd(cmd);
                }

            }
        }else if(instruct1.equals("44")&&instruct2.equals("2")){
            //设备开始或继续
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新开始状态  "+(mRunStatus == SportStatus.RUNNING));
            if(mRunStatus == SportStatus.RUNNING){
                return;
            }
            mRunStatus = SportStatus.RUNNING;
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新开始状态");
            HashMap<String, Object> map = new HashMap<>();
            map.put(EventBusEvent.RUNNING, 0);
            EventBus.getDefault().post(map);
            if(DeviceTypeUtils.connectDeviceType() == 0){
                BluetoothOrder.getInstance().getEquipmentState();//获取设备状态
            }
        }else if(instruct1.equals("44")&&instruct2.equals("3")){
            //设备暂停
            if(mRunStatus == SportStatus.PAUSE){
                return;
            }
            mRunStatus = SportStatus.PAUSE;
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新暂停状态");

            HashMap<String, Object> map = new HashMap<>();
            map.put(EventBusEvent.PAUSE_RUN, 0);
            EventBus.getDefault().post(map);
        }else if(instruct1.equals("44")&&instruct2.equals("4")){
            //设备停止
            if(mRunStatus == SportStatus.STOP){
                return;
            }
            mRunStatus = SportStatus.STOP;
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新停止状态");

            HashMap<String, Object> map = new HashMap<>();
            map.put(EventBusEvent.STOP_RUN, 0);
            EventBus.getDefault().post(map);
        }else if(instruct1.equals("44")&&instruct2.equals("5")){
            //设置阻力和坡度

        }else if(instruct1.equals("50")&&instruct2.equals("2")){
            //获取跑步机速度参数
            setTreadmillSpeed(value);

        }else if(instruct1.equals("50")&&instruct2.equals("3")){
            //获取跑步机坡度参数
            setTreadmillSlop(value);

        }else if(instruct1.equals("50")&&instruct2.equals("4")){
            //获取跑步机累计⾥程
            setTreadmillMileage(value);

        }else if(instruct1.equals("51")){
            //获取跑步机状态
            getTreadmillInfo(value);

        }else if(instruct1.equals("53")&&instruct2.equals("1")){
            //跑步机准备就绪
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知跑步机准备就绪状态");

            if(mRunStatus == SportStatus.START){
                return;
            }
            mRunStatus = SportStatus.START;
            int countdown = formatHexInt(Long.toString(value[3] & 0xff, 16));
            if (countdown > 0) {
                HashMap<String, Object> map = new HashMap<>();
                map.put(EventBusEvent.START_RUN, countdown);
                EventBus.getDefault().post(map);
            }else {
                HashMap<String, Object> map = new HashMap<>();
                map.put(EventBusEvent.RUNNING, 0);
                EventBus.getDefault().post(map);
                if(DeviceTypeUtils.connectDeviceType() == 0){
                    BluetoothOrder.getInstance().getTreadmillState();
                }
            }

        }else if(instruct1.equals("53")&&instruct2.equals("2")){
            //跑步机设置速度和坡度指令
            treadmillSpeedAndSlope(value);
//            SportsStatusBarViewUtils.getInstance().isSpeed(false);
//            SportsStatusBarViewUtils.getInstance().isSlop(false);

        }else if(instruct1.equals("53")&&instruct2.equals("3")){
            if(value.length >6){
                return;
            }
            //跑步机停止指令
//            if(mRunStatus == SportStatus.STOP){
//                return;
//            }
//            mRunStatus = SportStatus.STOP;
//            System.out.println("ddddddddddddddddddddddd   通知界面刷新停止状态");
//            HashMap<String, Object> map = new HashMap<>();
//            map.put(EventBusEvent.STOP_RUN, 0);
//            EventBus.getDefault().post(map);
        }else if(instruct1.equals("53")&&instruct2.equals("9")){
            //跑步机开始指令
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知跑步机刷新开始状态  "+(mRunStatus == SportStatus.RUNNING));

            if(mRunStatus == SportStatus.RUNNING){
                return;
            }
            mRunStatus = SportStatus.RUNNING;
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新开始状态");

            HashMap<String, Object> map = new HashMap<>();
            map.put(EventBusEvent.RUNNING, 0);
            EventBus.getDefault().post(map);

        }else if(instruct1.equals("53")&&instruct2.equals("A")){
            if(value.length >6){
                return;
            }
            //跑步机暂停指令
            if(mRunStatus == SportStatus.PAUSE){
                return;
            }
//            mRunStatus = SportStatus.PAUSE;
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新暂停状态");

//            HashMap<String, Object> map = new HashMap<>();
//            map.put(EventBusEvent.PAUSE_RUN, 0);
//            EventBus.getDefault().post(map);
        }
    }

    public void setHeartRate(int heartRate){
        this.heartRate = heartRate;
    }

    /**
     * 设备参数
     * @param value
     */
    public void equipmentInfo(byte[] value){
        int resistance = formatHexInt(Long.toString(value[3] & 0xff, 16));
        int slope = formatHexInt(Long.toString(value[4] & 0xff, 16));
        int i = formatHexInt(Long.toString(value[5] & 0xff, 16));//配置
//        String s = Integer.toBinaryString(i);
//        System.out.println(TAG+" s : "+s);
        int unitType = 0;
        int isPause = 0;
        if(i == 0){
            unitType = 0;
            isPause = 0;
        }else if(i == 1){
            unitType = 1;
            isPause = 0;
        }else if(i == 2){
            unitType = 0;
            isPause = 1;
        }else if(i == 3){
            unitType = 1;
            isPause = 1;
        }
//        if(s.length() >= 8){
//            unitType = Integer.valueOf(s.charAt(0));
//            isPause = Integer.valueOf(s.charAt(1));
//        }

        EquipmentInfoBean equipmentInfoBean = new EquipmentInfoBean();
        equipmentInfoBean.setResistance(resistance);
        equipmentInfoBean.setSlope(slope);
        equipmentInfoBean.setIsPause(isPause);
        equipmentInfoBean.setUnitType(unitType);
        DataManager.Companion.getInstance().saveUnit((unitType==1));

        getCurrentInfo().resistance = resistance;
        getCurrentInfo().slop = slope;
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG +" ddddddddddddddddddddddd "+equipmentInfoBean.toString());


        SettingUtils.getInstance().saveEquipmentInfo(GsonUtils.toJson(equipmentInfoBean));

    }


    /**
     * 设备状态
     * @param value
     */
    public void getEquipmentState(byte[] value){
        if(value.length <= 8){
            return;
        }
        int stats = formatHexInt(Long.toString(value[2] & 0xff, 16));//状态
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG +"  ddddddddddddddddddddddd   stats : "+GsonUtils.toJson(getCurrentInfo()));

        if(stats == 3 || stats == 14){
            return;
        }
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats  : "+stats+"       "+ mRunStatus);

        int speed1 = formatHexInt(Long.toString(value[3] & 0xff, 16));//速度
        int speed2 = formatHexInt(Long.toString(value[4] & 0xff, 16));//速度
        int speed = ((speed2*256)+speed1);//速度
        int resistance = formatHexInt(Long.toString(value[5] & 0xff, 16));//阻力
        int rpm1 = formatHexInt(Long.toString(value[6] & 0xff, 16));//当前RPM
        int rpm2 = formatHexInt(Long.toString(value[7] & 0xff, 16));//当前RPM
        int rpm = (rpm2*256)+rpm1;//当前RPM
        int heartRate = formatHexInt(Long.toString(value[8] & 0xff, 16));//当前心率
        int power1 = formatHexInt(Long.toString(value[9] & 0xff, 16));//当前功率
        int power2 = formatHexInt(Long.toString(value[10] & 0xff, 16));//当前功率
        int power = ((power2*256)+power1)/10;//当前功率
        int slope = formatHexInt(Long.toString(value[11] & 0xff, 16));//当前坡度
        int reserved = formatHexInt(Long.toString(value[12] & 0xff, 16));//预留

        EquipmentStateBean equipmentStateBean = new EquipmentStateBean();
        equipmentStateBean.setSpeed(speed);
        equipmentStateBean.setResistance(resistance);
        equipmentStateBean.setRpm(rpm);
        equipmentStateBean.setHeartRate(heartRate);
        equipmentStateBean.setPower(power);
        equipmentStateBean.setSlope(slope);
        equipmentStateBean.setReserved(reserved);

        getCurrentInfo().speed = speed;
        getCurrentInfo().slop = resistance;
        getCurrentInfo().heart = heartRate;
        getCurrentInfo().rpm = rpm;
        getCurrentInfo().power = power;

        LogUtils.getInstance().l(LogUtils.HINT,TAG+"  EquipmentState : "+GsonUtils.toJson(getCurrentInfo()));

        HashMap<String, Object> map = new HashMap<>();
        map.put(EventBusEvent.EQUIPMENT_STATE,getCurrentInfo());
        EventBus.getDefault().post(map);
    }

    /**
     * 跑步机运动数据和状态
     * @param value
     */
    public void getTreadmillInfo(byte[] value){
        HashMap<String, Object> map = new HashMap<>();
        if(value.length < 5){
            return;
        }
        int stats = formatHexInt(Long.toString(value[2] & 0xff, 16));//状态
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats  : "+stats+"       "+ mRunStatus);

        if(stats == 4){
            SportsStatusBarViewUtils.getInstance().setEnabled(false);
        }else {
            SportsStatusBarViewUtils.getInstance().setEnabled(true);
        }
        if(stats == 6){
            int type = formatHexInt(Long.toString(value[3] & 0xff, 16));
            if(type == 2){
                LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats    跑步机睡眠中");

//                ToastUtils.showShort("跑步机休眠中");
            }else if(type == 1){
                LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats    跑步机安全锁中");
//                ToastUtils.showShort("跑步机安全锁中");
                map.put(EventBusEvent.SAFETY_LOCK,true);
                EventBus.getDefault().post(map);
            }
            return;
        }else if(stats == 0||stats == 9){
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats    跑步机停止" + mRunStatus);

            map.put(EventBusEvent.SAFETY_LOCK,false);
            EventBus.getDefault().post(map);
            if(mRunStatus == SportStatus.STOP){
                return;
            }
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   通知界面刷新停止状态");

            mRunStatus = SportStatus.STOP;
            map.put(EventBusEvent.STOP_RUN,0);
            EventBus.getDefault().post(map);
            return;
        }else if(stats == 2){
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats    跑步机启动中"+mRunStatus);

            if(mRunStatus == SportStatus.START){
                return;
            }
            int count = formatHexInt(Long.toString(value[3] & 0xff, 16));
//            mRunStatus = SportStatus.START;
            map.put(EventBusEvent.START_RUN,count);
            EventBus.getDefault().post(map);

//            int count = formatHexInt(Long.toString(value[3] & 0xff, 16));
//            map.put(EventBusEvent.START_RUN,count);
//            EventBus.getDefault().post(map);
//            System.out.println("ddddddddddddddddddddddd    跑步机启动中 11");
            return;
        }else if(stats == 10){
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd    stats    跑步机暂停" + mRunStatus);

            if(mRunStatus == SportStatus.PAUSE){
                return;
            }
            mRunStatus = SportStatus.PAUSE;
//            SerialPortManager.getInstance().setRunStatus(SportStatus.PAUSE);
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   串口通知界面刷新暂停状态");

            map.put(EventBusEvent.PAUSE_RUN,0);
            EventBus.getDefault().post(map);

        }else if(stats == 3||stats == 4){
            if(stats == 3){
                if(mRunStatus != SportStatus.RUNNING){
                    mRunStatus = SportStatus.RUNNING;
                }

            }
            if(value.length <= 16){
                LogUtils.getInstance().l(LogUtils.MOTION_DATA,"ddddddddddddddddddddddd   串口运动中数据长度不足17，数据异常");
                return;
            }
            int speed = formatHexInt(Long.toString(value[3] & 0xff, 16));//速度
            int slop = formatHexInt(Long.toString(value[4] & 0xff, 16));//坡度
            int time1 = formatHexInt(Long.toString(value[5] & 0xff, 16));//时间
            int time2 = formatHexInt(Long.toString(value[6] & 0xff, 16));//时间
            int time = (time2*256)+time1;//时间
            int distance1 = formatHexInt(Long.toString(value[7] & 0xff, 16));//距离
            int distance2 = formatHexInt(Long.toString(value[8] & 0xff, 16));//距离
            int distance = (distance2*256)+distance1;//距离
            int heat1 = formatHexInt(Long.toString(value[9] & 0xff, 16));//热量
            int heat2 = formatHexInt(Long.toString(value[10] & 0xff, 16));//热量
            int heat = (heat2*256)+heat1;//热量
            int step1 = formatHexInt(Long.toString(value[11] & 0xff, 16));//步数
            int step2 = formatHexInt(Long.toString(value[12] & 0xff, 16));//步数
            int step = (step2*256)+step1;//步数
            int heart1 = formatHexInt(Long.toString(value[13] & 0xff, 16));//心率
            int heart2 = formatHexInt(Long.toString(value[14] & 0xff, 16));//心率
            int heart = 0;
            if(heartRate > 0){
                heart = heartRate;
            }else {
                heart = (heart2*256)+heart1;//心率
            }

            getCurrentInfo().speed = speed;
            getCurrentInfo().slop = slop;
            getCurrentInfo().count = step;
            getCurrentInfo().heart = heart;
            getCurrentInfo().kcal = heat;
            getCurrentInfo().distance = distance;
            getCurrentInfo().time = time;

//            System.out.println(TAG+" ddddddddddddddddddddddd   TreadmillState : "+GsonUtils.toJson(getCurrentInfo()));
            map.put(EventBusEvent.EQUIPMENT_STATE,"");
            EventBus.getDefault().post(map);
        }
    }

    /**
     * 获取品牌信息
     */
    public void getBrandInfo(byte[] value){
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" getBrandInfo :  value : "+ByteUtils.byteToString(value)+"  value[4] : "+value[4]);

        String str = Long.toString(value[4] & 0xff, 16);
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" getBrandInfo :  str : "+str);

        if(DeviceTypeUtils.TREADMILL.equals(str) || DeviceTypeUtils.WALKING_TREADMILL.equals(str)){
            //跑步机
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" 跑步机 ");

            mCurrentDeviceType = DeviceTypeUtils.TREADMILL;

        }else if(DeviceTypeUtils.ELLIPTICAL_MACHINE.equals(str)){
            //椭圆机
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" 椭圆机 ");

            mCurrentDeviceType = DeviceTypeUtils.ELLIPTICAL_MACHINE;
        }else if(DeviceTypeUtils.SPINNING.equals(str)){
            //动感单车
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" 动感单车 ");

            mCurrentDeviceType = DeviceTypeUtils.SPINNING;
        } else if (DeviceTypeUtils.ROWING_MACHINE.equals(str)) {
            //划船机
            LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" 划船机 ");

            mCurrentDeviceType = DeviceTypeUtils.ROWING_MACHINE;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put(EventBusEvent.REFRESH_FRAG,"");
        EventBus.getDefault().post(map);
        if (mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)) {
//            getTreadmillSpeed();
            SerialPortManager.Cmd cmd = new SerialPortManager.Cmd();
            cmd.bytes = BluetoothOrder.TREADMILL_GET_SPEED;
            SerialPortManager.getInstance().addCmd(cmd);
            Observable.timer(500, TimeUnit.MILLISECONDS)
                    .subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            coundDisposable = d;
                        }

                        @Override
                        public void onNext(Long aLong) {
                            if(DeviceTypeUtils.connectDeviceType() == 0){
                                BluetoothOrder.getInstance().getTreadmillSlop();
                            }else {
                                SerialPortManager.Cmd cmd = new SerialPortManager.Cmd();
                                cmd.bytes = BluetoothOrder.TREADMILL_GET_SLOP;
                                SerialPortManager.getInstance().addCmd(cmd);
                            }

                            closeCoundDisposable();
                        }

                        @Override
                        public void onError(Throwable e) {
                            closeCoundDisposable();
                        }

                        @Override
                        public void onComplete() {
                            closeCoundDisposable();
                        }
                    });
        }else {
            if(DeviceTypeUtils.connectDeviceType() == 0){
                BluetoothOrder.getInstance().blueEquipmentParameter();
            }else {
                SerialPortManager.Cmd cmd = new SerialPortManager.Cmd();
                cmd.bytes = BluetoothOrder.EQUIPMENT_GET_EQUIPMENT_PARAMETER;
                SerialPortManager.getInstance().addCmd(cmd);
            }

        }

    }

    /**
     * 运动数据
     * @param value
     */
    public void getSportsData(byte[] value){
        if(value.length <= 6){
            return;
        }
        int time1 = formatHexInt(Long.toString(value[3] & 0xff, 16));
        int time2 = formatHexInt(Long.toString(value[4] & 0xff, 16));
        int time = (time2*256)+time1;
        int distance1 = formatHexInt(Long.toString(value[5] & 0xff, 16));
        int distance2 = formatHexInt(Long.toString(value[6] & 0xff, 16));
        int distance = (distance2*256) + distance1;
        int heat1 = formatHexInt(Long.toString(value[7] & 0xff, 16));
        int heat2 = formatHexInt(Long.toString(value[8] & 0xff, 16));
        int heat = (heat2*256)+heat1;
        int num1 = formatHexInt(Long.toString(value[9] & 0xff, 16));
        int num2 = formatHexInt(Long.toString(value[10] & 0xff, 16));
        int num = (num2*256)+num1;
        SportsDataBean sportsDataBean = new SportsDataBean();
        sportsDataBean.setTime(time);
        sportsDataBean.setDistance(distance);
        sportsDataBean.setHeat(heat);
        sportsDataBean.setNum(num);

        getCurrentInfo().distance = distance;
        getCurrentInfo().kcal = heat;
        getCurrentInfo().count = num;
        getCurrentInfo().time = time;
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" SportsData : "+GsonUtils.toJson(getCurrentInfo()));
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" SportsDataBean : "+sportsDataBean.toString());


        HashMap<String, Object> map = new HashMap<>();
        map.put(EventBusEvent.SPORTS_DATA,getCurrentInfo());
        EventBus.getDefault().post(map);
    }

    /**
     * 获取跑步机速度
     * @param value
     */
    public void setTreadmillSpeed(byte[] value){
        int speedMax = formatHexInt(Long.toString(value[3] & 0xff, 16));//速度
        int speedMin = formatHexInt(Long.toString(value[4] & 0xff, 16));//速度

        EquipmentInfoBean equipmentInfoBean = EquipmentInfoUtils.getInstance().getEquipmentInfoBean();
        equipmentInfoBean.setResistance(speedMax);
        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" ddddddddddddddddddddddd  TreadmillSpeed  "+speedMax);


        EquipmentInfoUtils.getInstance().saveEquipmentInfoBean(GsonUtils.toJson(equipmentInfoBean));

    }

    /**
     * 获取跑步机坡度
     * @param value
     */
    public void setTreadmillSlop(byte[] value){
        int slopMax = formatHexInt(Long.toString(value[3] & 0xff, 16));//坡度
        int slopMin = formatHexInt(Long.toString(value[4] & 0xff, 16));//坡度
        int config = formatHexInt(Long.toString(value[5] & 0xff, 16));//配置
        int unitType = 0;
        int isPause = 0;
        if(config == 0){
            unitType = 0;
            isPause = 0;
        }else if(config == 1){
            unitType = 1;
            isPause = 0;
        }else if(config == 2){
            unitType = 0;
            isPause = 1;
        }else if(config == 3){
            unitType = 1;
            isPause = 1;
        }

        EquipmentInfoBean equipmentInfoBean = EquipmentInfoUtils.getInstance().getEquipmentInfoBean();
        equipmentInfoBean.setSlope(slopMax);
        equipmentInfoBean.setIsPause(isPause);
        equipmentInfoBean.setUnitType(unitType);
        DataManager.Companion.getInstance().saveUnit((unitType==1));

        LogUtils.getInstance().l(LogUtils.MOTION_DATA,TAG+" ddddddddddddddddddddddd  TreadmillSlop "+ equipmentInfoBean.toString());

        EquipmentInfoUtils.getInstance().saveEquipmentInfoBean(GsonUtils.toJson(equipmentInfoBean));
    }

    /**
     * 获取跑步机里程
     * @param value
     */
    public void setTreadmillMileage(byte[] value){
        int mileage1 = formatHexInt(Long.toString(value[3] & 0xff, 16));//里程
        int mileage2 = formatHexInt(Long.toString(value[4] & 0xff, 16));//里程
        int mileage3 = formatHexInt(Long.toString(value[5] & 0xff, 16));//里程
        int mileage4 = formatHexInt(Long.toString(value[6] & 0xff, 16));//里程
    }

    /**
     * 设置跑步机的速度和坡度
     * @param value
     */
    public void  treadmillSpeedAndSlope(byte[] value){
        int speed = formatHexInt(Long.toString(value[3] & 0xff, 16));//速度
        int slop = formatHexInt(Long.toString(value[4] & 0xff, 16));//坡度
        getCurrentInfo().speed = speed;
        getCurrentInfo().slop = slop;

    }

    public void setDeviceSpeed(int speed){
        deviceSpeed = speed;
    }

    /**
     * 是否正在运行
     * @return
     */
    public boolean isRun() {
        return mRunStatus == SportStatus.START || mRunStatus == SportStatus.RUNNING ;
    }

    public boolean isStart() {
        return mRunStatus == SportStatus.START;
    }
    public boolean isPause() {
        return mRunStatus == SportStatus.PAUSE;
    }
    public boolean isStop() {
        return mRunStatus == SportStatus.STOP;
    }


    public void closeCoundDisposable(){
        if(coundDisposable != null){
            coundDisposable.dispose();
            coundDisposable = null;
        }
    }

    public SerialPortManager.CurrentInfo getCurrentInfo(){
        if (mCurrentInfo == null){
            return mCurrentInfo = new SerialPortManager.CurrentInfo();
        }
        return mCurrentInfo;
    }

    /**
     * 清除运动数据
     */
    public void closeCurrentInfo(){
        if(mCurrentInfo != null){
            mCurrentInfo = null;
        }
    }

    /**
     * 十六进制转十进制
     * @param str
     * @return
     */
    public int formatHexInt(String str){
        int decimalNumber = 0;
        try {
//            System.out.println(TAG+"  formatHexInt : "+str);
            decimalNumber = Integer.parseInt(str, 16);
        }catch (Exception e){
            e.printStackTrace();
        }
        return decimalNumber;
    }

}
